﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace HDJ.Framework.Core {
    public class IWindowBase : MonoBehaviour
    {
        public UIWindowSetting windowSetting = new UIWindowSetting();
        public ObjectReference objectReference;

        public string windowName
        {
            get
            {
               return GetType().Name;
            }
        }


        public UIWindowState state = UIWindowState.Closed;


        public GameObject GetGameObject(string name)
        {
            return objectReference.GetGameObject(name);
        }

        public T GetComponent<T>(string name) where T : Object
        {
            GameObject go = GetGameObject(name);
            if (go)
            {
                return go.GetComponent<T>();
            }
            return null;
        }
        public T[] GetComponents<T>(string name) where T : Object
        {
            GameObject go = GetGameObject(name);
            if (go)
            {
                return go.GetComponents<T>();
            }
            return null;
        }

        private Dictionary<string, List<UnityAction>> buttonCallBackBuff = new Dictionary<string, List<UnityAction>>();


        public void AddButtonListener(string name, UnityAction callBack)
        {
            Button bt = null;
            if (buttonCallBackBuff.ContainsKey(name))
            {
                if (buttonCallBackBuff[name].Contains(callBack))
                    return;

            }
            bt = GetComponent<Button>(name);
            bt.onClick.AddListener(callBack);
            if (buttonCallBackBuff.ContainsKey(name))
            {
                buttonCallBackBuff[name].Add(callBack);
            }
            else
            {
                buttonCallBackBuff.Add(name, new List<UnityAction>() { callBack });
            }

        }
        public void RemoveButtonListener(string name, UnityAction callBack)
        {
            Button bt = null;
            if (buttonCallBackBuff.ContainsKey(name))
            {
                if (buttonCallBackBuff[name].Contains(callBack))
                {
                    buttonCallBackBuff[name].Remove(callBack);

                    bt = GetComponent<Button>(name);
                    bt.onClick.RemoveListener(callBack);

                    if (buttonCallBackBuff[name].Count == 0)
                        buttonCallBackBuff.Remove(name);
                }

            }
        }
        public void RemoveButtonAllListener(string name)
        {
            Button bt = null;
            if (buttonCallBackBuff.ContainsKey(name))
            {
                bt = GetComponent<Button>(name);
                bt.onClick.RemoveAllListeners();

                buttonCallBackBuff.Remove(name);
            }
        }

        #region virtual api
        /// <summary>
        /// 只在创建实例时调用一次
        /// </summary>
        public virtual void OnAwake() { }
        /// <summary>
        /// 每次打开Window调用
        /// </summary>
        /// <param name="param"></param>
        protected virtual void OnOpen(params object[] param) { }
        public virtual void OnOpenAfterAnimation() { }
        public virtual void OnExit() { }
        protected virtual void OnExitAfterAnimation() { }

        public virtual IEnumerator OpenAnimation()
        {
            yield return null;
        }
        public virtual IEnumerator ExitAnimation()
        {
            yield return null;
        }


        #endregion

        #region internal api

        internal void Init(params object[] param)
        {
            //if (windowSetting.root)
            //    windowSetting.root.SetActive(true);
            //if (windowSetting.backGroundRoot)
            //    windowSetting.backGroundRoot.SetActive(true);
            gameObject.SetActive(true);
            OnOpen(param);
        }

        internal void Closed()
        {
            //if (windowSetting.root)
            //    windowSetting.root.SetActive(false);
            //if (windowSetting.backGroundRoot)
            //    windowSetting.backGroundRoot.SetActive(false);
            gameObject.SetActive(false);
            OnExitAfterAnimation();
        }
    
        #endregion


    }
}
